/**
 * storage 二次封装
 */

const NAME_SPACE = 'eblog'
const storage = {
  // 设置
  setItem(key, val) {
    const storage = this.getStorage()
    storage[key] = val
    window.localStorage.setItem(NAME_SPACE, JSON.stringify(storage))
  },

  // 获取
  getItem(key) {
    return this.getStorage()[key]
  },

  // 清除
  clearItem(key) {
    const storage = this.getStorage()
    delete storage[key]
    window.localStorage.setItem(NAME_SPACE, JSON.stringify(storage))
  },

  // 清除全部
  clearAll() {
    window.localStorage.clearAll()
  },

  getStorage() {
    return JSON.parse(window.localStorage.getItem(NAME_SPACE) || '{}')
  }
}

export default storage
